package tps12.axwax.server.data;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import junit.framework.TestCase;

import com.google.appengine.api.datastore.dev.LocalDatastoreService;
import com.google.appengine.tools.development.ApiProxyLocalImpl;
import com.google.apphosting.api.ApiProxy;

public class LocalDatastoreTestCase extends TestCase {
	
	@Override
	public void setUp() throws Exception {
		super.setUp();
        ApiProxy.setEnvironmentForCurrentThread(new TestEnvironment());
        ApiProxy.setDelegate(new ApiProxyLocalImpl(new File(".")){});
        ApiProxyLocalImpl proxy = (ApiProxyLocalImpl) ApiProxy.getDelegate();
        proxy.setProperty(LocalDatastoreService.NO_STORAGE_PROPERTY, Boolean.TRUE.toString());
 	}
	
	@Override
	public void tearDown() throws Exception {
		ApiProxyLocalImpl proxy = (ApiProxyLocalImpl)ApiProxy.getDelegate();
        LocalDatastoreService datastoreService = (LocalDatastoreService) proxy.getService("datastore_v3");
        datastoreService.clearProfiles();
        ApiProxy.setDelegate(null);
        ApiProxy.setEnvironmentForCurrentThread(null);
        
		super.tearDown();
	}
			
	public static class TestEnvironment implements ApiProxy.Environment {
		public static boolean isLoggedIn = false;
		public static boolean isAdmin = false;
		
		  public String getAppId() {
		    return "test";
		  }

		  public String getVersionId() {
		    return "1.0";
		  }

		  public String getEmail() {
		    return "test@example.com";
		  }

		  public boolean isLoggedIn() {
		    return isLoggedIn;
		  }

		  public boolean isAdmin() {
		    return isAdmin;
		  }

		  public String getAuthDomain() {
		    return "test";
		  }

		  public String getRequestNamespace() {
		    return "";
		  }

		  public Map<String, Object> getAttributes() {
		    return new HashMap<String, Object>();
		  }
	}		
}